#!/bin/bash
sudo apt install -y byobu sudo net-tools ncdu mc nginx zip apt-transport-https ca-certificates curl gnupg-agent software-properties-common software-properties-common dirmngr
cd ~
mkdir -p /home/seastone/rackServer
cd /home/seastone/rackServer
wget -O ksa.zip https://seastone-1252912192.cos.ap-guangzhou.myqcloud.com/ksa.zip
unzip ksa.zip -d /home/seastone/rackServer
chmod +x ksa.sh && chmod +x ksa_x64
sudo ln -s /home/seastone/rackServer/ksa.sh /etc/init.d/ksa
sudo update-rc.d ksa defaults 99

sudo apt-get remove docker docker-engine docker.io containerd runc
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable"
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/debian buster main'

sudo apt-get update -y
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://1nj0zren.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo usermod -aG docker seastone

sudo apt-get install -y mariadb-server

wget https://www.emqx.io/cn/downloads/broker/v4.2.5/emqx-debian10-4.2.5-x86_64.deb
sudo dpkg -i emqx-debian10-4.2.5-x86_64.deb
sudo systemctl enable emqx
sudo systemctl start emqx

mkdir -p /home/seastone/seq/data
sudo docker run --name seq -d --restart unless-stopped --memory=8g --memory-swap=16g -e ACCEPT_EULA=Y -v /home/seastone/seq/data:/data -p 5380:80 -p 5341:5341 datalust/seq:latest
sudo docker volume create portainer_data
sudo docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer 
sudo mkdir /opt/dotnet
wget -O dotnet-sdk.tar.gz https://download.visualstudio.microsoft.com/download/pr/5de23f6d-648c-455b-a7a9-d11c9a5bca40/4836262466f2d288e8ad8647944d062f/dotnet-sdk-3.1.405-linux-x64.tar.gz
sudo tar zxfv dotnet-sdk.tar.gz -C /opt/dotnet
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
dotnet --info

sudo tee /etc/nginx/sites-enabled/rackServer <<-'EOF'
server {
        listen 5006;
        listen 5080;
        server_name _;
        location / {               
          proxy_pass         http://localhost:5000;
          proxy_http_version 1.1;
          proxy_set_header   Upgrade $http_upgrade;
          proxy_set_header   Connection keep-alive;
          proxy_set_header   Host $host;
          proxy_cache_bypass $http_upgrade;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Proto $scheme;
          client_max_body_size 200m;
        }
}
EOF
sudo tee /etc/mysql/my.cnf  <<-'EOF'
# MariaDB-specific config file.
# Read by /etc/mysql/my.cnf

[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8 

[mysqld]
#
# * Character sets
# 
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
lower_case_table_names = 1
character-set-server  = utf8 
collation-server      = utf8_general_ci 
character_set_server   = utf8 
collation_server       = utf8_general_ci 
# Import all .cnf files from configuration directory

!includedir /etc/mysql/mariadb.conf.d/
EOF
sudo tee /etc/nginx/sites-enabled/updater <<-'EOF'
server {
        listen 9081 default_server;
        listen [::]:9081 default_server;
        root /home/seastone/update_pkg;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}
EOF

sudo systemctl start nginx
sudo nginx -t
sudo nginx -s reload

sudo tee /etc/systemd/system/rackServer.service <<-'EOF'
#查看日志 sudo journalctl -fu rackServer.service
#查看日志 筛选 sudo journalctl -fu rackServer.service --since "2016-10-18" --until "2016-10-18 04:00"
[Unit]
Description=Seastone Smart Rack WebApi Service

[Service]
WorkingDirectory=/home/seastone/rackServer
ExecStart=dotnet /home/seastone/rackServer/SmartRack.Web.Api.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=SmartRackServer
User=seastone
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable rackServer.service
sudo systemctl start rackServer.service